<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"matian.connorma.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="在mysql 5.6之前的版本，MyISAM引擎支持全文索引，但是InnoDB不支持；从mysql 5.6开始，InnoDB支持全文索引，但是不支持中文分词；从mysql 5.7.6开始，mysql内置了ngram分词器，可支持中文、日语等亚洲语种的分词。 全文索引fulltext index，可用于char&#x2F;varchar&#x2F;text列。在需要进行模糊查询时，如果使用 like">
<meta property="og:type" content="article">
<meta property="og:title" content="mysql全文索引的使用">
<meta property="og:url" content="http://matian.connorma.cn/cfe640d.html">
<meta property="og:site_name" content="马天">
<meta property="og:description" content="在mysql 5.6之前的版本，MyISAM引擎支持全文索引，但是InnoDB不支持；从mysql 5.6开始，InnoDB支持全文索引，但是不支持中文分词；从mysql 5.7.6开始，mysql内置了ngram分词器，可支持中文、日语等亚洲语种的分词。 全文索引fulltext index，可用于char&#x2F;varchar&#x2F;text列。在需要进行模糊查询时，如果使用 like">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-12-09T07:38:43.000Z">
<meta property="article:modified_time" content="2024-05-26T11:48:06.731Z">
<meta property="article:author" content="马天">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://matian.connorma.cn/cfe640d.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>mysql全文索引的使用 | 马天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">马天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://matian.connorma.cn/cfe640d.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="马天">
      <meta itemprop="description" content="程序猿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="马天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          mysql全文索引的使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-12-09 15:38:43" itemprop="dateCreated datePublished" datetime="2020-12-09T15:38:43+08:00">2020-12-09</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/mysql/" itemprop="url" rel="index"><span itemprop="name">mysql</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在mysql 5.6之前的版本，MyISAM引擎支持全文索引，但是InnoDB不支持；从mysql 5.6开始，InnoDB支持全文索引，但是不支持中文分词；从mysql 5.7.6开始，mysql内置了ngram分词器，可支持中文、日语等亚洲语种的分词。</p>
<p>全文索引fulltext index，可用于char&#x2F;varchar&#x2F;text列。在需要进行模糊查询时，如果使用 like “%target%” 进行查询，无法使用到索引。如果文本较大，为了优化效率，可以考虑全文索引。</p>
<p>本文将依次介绍以下内容：</p>
<ul>
<li>分词与倒排索引，了解全文索引的基本原理</li>
<li>mysql分词器配置与全文索引的创建</li>
<li>全文索引的使用示例</li>
</ul>
<span id="more"></span>

<h2 id="分词与倒排索引"><a href="#分词与倒排索引" class="headerlink" title="分词与倒排索引"></a>分词与倒排索引</h2><p>我们在数据结构学习时就了解到，倒排索引的典型应用场景是搜索引擎。为了快速的根据文本找出包含此文本的文档，那么需要解决两个基本的问题：</p>
<ul>
<li>对文档进行分词，找出每个文档包含的“关键字”，称为token。比如：存在一个ID&#x3D;1的文档内容为“倒排索引的典型应用场景是搜索引擎”，存在的一种分词结果可以是 “倒排索引&#x2F;的&#x2F;典型&#x2F;应用&#x2F;场景&#x2F;是&#x2F;搜索引擎”，如此经过分即能得到这一段文本的关键字</li>
<li>构建倒排索引表，记录 关键字-所属文档 的这一基本的关联关系，以及：关键字出现次数，即词频；关键字在文档中所处的位置等。接上条，将会存在 “倒排索引” -&gt; (ID &#x3D; 1) 这一索引记录</li>
</ul>
<p>对于分词，这是一个算法领域的问题，鄙人不敢说有所了解，大家可以自行网上学习。所幸这并不影响我们的使用层面，了解达到的效果即可。</p>
<p>如此，构建出倒排索引表后，一个检索过程包含的基本步骤有：</p>
<ol>
<li>对每条输入，我们对输入的内容进行分词，比如：全文索引，分词得到”全文”、”索引”两个token</li>
<li>在倒排索引表中进行匹配，查找出token匹配到的所有文档</li>
<li>根据匹配结果，可以找出匹配到token最多的文档；同时可以根据token在文档中的词频评估文档与token的相关度</li>
<li>通过一系列评估、排序算法得出文档最终的相关性score，得到最终结果</li>
</ol>
<p>倒排索引对大量文本情况下关键字到文档的检索非常有效。但是构建的过程是比较复杂的（对比正向索引）；关键的是在文档更新时倒排索引表的更新会很复杂，对现有倒排索引表更新的逻辑复杂，重建倒排索引表虽然逻辑相对而言简单，但是代价高。</p>
<h2 id="mysql分词器配置与全文索引的创建"><a href="#mysql分词器配置与全文索引的创建" class="headerlink" title="mysql分词器配置与全文索引的创建"></a>mysql分词器配置与全文索引的创建</h2><p>mysql的全文索引即是倒排索引的应用。</p>
<p>在建表时可以指定添加全文索引：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `test_fulltext_idx_tbl` (</span><br><span class="line">  `id` <span class="type">bigint</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;文档ID&#x27;</span>,</span><br><span class="line">  `doc_name` <span class="type">varchar</span>(<span class="number">255</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_bin <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;&#x27;</span> COMMENT <span class="string">&#x27;文档名&#x27;</span>,</span><br><span class="line">  `content` text <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_bin COMMENT <span class="string">&#x27;文档内容&#x27;</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE,</span><br><span class="line">  FULLTEXT KEY `ft_search_index` (`doc_name`,`content`) <span class="comment">/*!50100 WITH PARSER `ngram` */</span> </span><br><span class="line">) ENGINE<span class="operator">=</span>InnoDB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4 <span class="keyword">COLLATE</span><span class="operator">=</span>utf8mb4_bin ROW_FORMAT<span class="operator">=</span><span class="keyword">DYNAMIC</span> COMMENT<span class="operator">=</span><span class="string">&#x27;全文索引测试表&#x27;</span></span><br></pre></td></tr></table></figure>
<p>指定了全文索引在mysql 5.7以上使用ngram分词器</p>
<p>但是如上对倒排索引的分析，倒排索引的更新复杂、代价高，因此在一个频繁修改的字段上使用全文索引会影响性能。因此，相比于建表同时创建索引、然后导入数据，更为高效的方式是建表时不添加全文索引、在数据导入完成后再添加全文索引：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> test_fulltext_idx_tbl <span class="keyword">ADD</span> FULLTEXT ft_search_index(doc_name, content) <span class="keyword">WITH</span> parser ngram;</span><br></pre></td></tr></table></figure>

<p>对于ngram，需要关注的是mysql的ngram_token_size这一配置。ngram_token_size表示ngram分词器分词的最小字数，默认为2，此时使用全文索引匹配单个字是无法匹配成功的。</p>
<p>这一配置可以在mysql的cnf文件中设置：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[mysqld] </span><br><span class="line">ngram_token_size=2</span><br></pre></td></tr></table></figure>

<p>因为中文词最少包含两个字，因此一般使用默认值2即可。</p>
<h2 id="全文索引的使用示例"><a href="#全文索引的使用示例" class="headerlink" title="全文索引的使用示例"></a>全文索引的使用示例</h2><p>全文索引使用的语法为：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">MATCH</span> (col1,col2,...) AGAINST (expr [search_modifier])</span><br></pre></td></tr></table></figure>
<p>其中，MATCH指定的字段必须与使用的全文索引的字段一致，expr即检索的关键字，search_modifier可指定两种查询模式：</p>
<ul>
<li>IN NATURAL LANGUAGE MODE，默认的模式，自然语言模式，根据分词模糊匹配</li>
<li>IN BOOLEAN MODE，布尔模式，可以在表达式中使用操作符，如 ‘+能力 -审核”，表示必须存在“能力”、必须不存在“审核”</li>
</ul>
<p>在上文的测试表test_fulltext_idx_tbl中添加测试数据并对doc_name、content两个字段创建全文索引</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `test_fulltext_idx_tbl` (`id`, `doc_name`, `content`) <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;入门指南&#x27;</span>, <span class="string">&#x27;赋能开放平台目前已接入了各种技术能力，各个子应用根据以下步骤即可快速接入：</span></span><br><span class="line"><span class="string">创建应用</span></span><br><span class="line"><span class="string">登录赋能开放平台门户；</span></span><br><span class="line"><span class="string">依次点击控制台，应用列表，创建应用；</span></span><br><span class="line"><span class="string">填写应用名称，应用描述等信息，按需勾选能力；</span></span><br><span class="line"><span class="string">点击立即创建，等待管理员审核完成，一般在1个工作日内审核完成。</span></span><br><span class="line"><span class="string">获取访问Token</span></span><br><span class="line"><span class="string">依次点击控制台，应用列表；</span></span><br><span class="line"><span class="string">在我的应用列表中，复制APP ID和Secret Key；</span></span><br><span class="line"><span class="string">根据api鉴权文档，获取访问Token。</span></span><br><span class="line"><span class="string">调用能力接口</span></span><br><span class="line"><span class="string">在http请求header中access-token，请求能力的OpenAPI。&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `test_fulltext_idx_tbl` (`id`, `doc_name`, `content`) <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">&#x27;应用审核不通过常见原因&#x27;</span>, <span class="string">&#x27;审核过程中常见的驳回原因及处理方式：</span></span><br><span class="line"><span class="string">应用名称含有敏感词</span></span><br><span class="line"><span class="string">修改应用名称后重新提交申请。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">应用描述信息不完善</span></span><br><span class="line"><span class="string">重新编辑、完善描述信息&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `test_fulltext_idx_tbl` (`id`, `doc_name`, `content`) <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">&#x27;人工智能&#x27;</span>, <span class="string">&#x27;1. 人工智能的定义</span></span><br><span class="line"><span class="string">人工智能(AI)是指通过普通计算机程序来呈现人类智能的技术。AI是现代计算创新的支柱，可为个人和企业挖掘价值。AI是一个广博的领域，涵盖许多不同的学科，包括计算机科学、数据分析和统计、硬件和软件工程、语言学、神经学，甚至哲学和心理学。在业务使用的操作层面上，AI是一组主要基于机器学习和深度学习的技术，用于数据分析、预测、对象分类、自然语言处理、推荐、智能数据检索等等。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2. 人工智能的优势</span></span><br><span class="line"><span class="string">2.1 自动化</span></span><br><span class="line"><span class="string">AI可以自动执行工作流和流程，也可以不依靠人工团队来独立自主地开展工作。例如，AI可以通过持续监控和分析网络流量来帮助自动执行信息安全的各个方面。同样，智能工厂可能使用数十种不同类型的AI，例如机器人使用计算机视觉在工厂车间移动或检查产品是否存在缺陷、创建数字孪生体，或使用实时分析来衡量效率和产量。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.2 减少人为错误</span></span><br><span class="line"><span class="string">AI可以通过每次都遵循相同流程的自动化功能和算法来消除数据处理、分析、制造装配和其他任务中的人为错误。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.3 消除重复任务</span></span><br><span class="line"><span class="string">AI可用于执行重复任务，从而让人力资源能够空出手来解决影响较大的问题。AI可用于自动执行流程，例如验证文档、转录电话或回答“你们几点关门？”之类的简单客户问题。机器人通常用于代替人类执行“枯燥、肮脏或危险”的任务。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.4 快速准确</span></span><br><span class="line"><span class="string">与人类相比，AI可以更快地处理更多信息，从而查找模式并发现人类可能错过的数据关系。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.5 无限可用性</span></span><br><span class="line"><span class="string">AI不受时段、休息需求或其他人类负担的限制。在云端运行时，AI和机器学习可以“始终开启”，从而持续处理分配的任务。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.6 更快的研发速度</span></span><br><span class="line"><span class="string">快速分析大量数据的能力可以加快获得研发突破的速度。例如，AI已用于潜在新药物疗法的预测建模，或量化人类基因组。&#x27;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>使用以下语句可以通过全文索引进行检索：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	doc_name,</span><br><span class="line">	content,</span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST ( <span class="string">&#x27;审核&#x27;</span> ) <span class="keyword">AS</span> score </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	test_fulltext_idx_tbl </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST ( <span class="string">&#x27;审核&#x27;</span> )</span><br></pre></td></tr></table></figure>
<p>以上sql中score是文档的相关性得分，只有超过0的文档才认为匹配成功。这一查询将得到两条结果。</p>
<p>而因为我们使用了默认的ngram_token_size为2，即分词最小字数为2，所以可以看到以下sql得到所有文档的score均为0：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	doc_name,</span><br><span class="line">	content,</span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST ( <span class="string">&#x27;审&#x27;</span> ) <span class="keyword">AS</span> score </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	test_fulltext_idx_tbl </span><br></pre></td></tr></table></figure>

<p>如上文所述，检索的过程中将对输入的内容进行分词后再检索匹配，所以以下sql也能筛选出两条结果：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	doc_name,</span><br><span class="line">	content,</span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST ( <span class="string">&#x27;技术能力开放&#x27;</span> ) <span class="keyword">AS</span> score </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	test_fulltext_idx_tbl </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST (</span><br><span class="line">	<span class="string">&#x27;技术能力开放&#x27;</span> </span><br><span class="line">	)</span><br></pre></td></tr></table></figure>

<p>实际上，’技术能力开放’并没有作为整体出现在任何一个文档中，以下like查询的sql结果为0条可以验证：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	test_fulltext_idx_tbl </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	doc_name <span class="keyword">LIKE</span> &quot;%技术能力开放%&quot; </span><br><span class="line">	<span class="keyword">OR</span> content <span class="keyword">LIKE</span> &quot;%技术能力开放%&quot;</span><br></pre></td></tr></table></figure>

<p>那么如果解决需要严格匹配而不是模糊查询的情况呢？此时使用布尔模式查询配合操作符’+’就可以做到：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	doc_name,</span><br><span class="line">	content,</span><br><span class="line">	<span class="keyword">MATCH</span> ( doc_name, content ) AGAINST ( <span class="string">&#x27;+技术能力开放&#x27;</span> <span class="keyword">IN</span> <span class="type">BOOLEAN</span> MODE) <span class="keyword">AS</span> score </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	test_fulltext_idx_tbl </span><br></pre></td></tr></table></figure>
<p>可以看到，这一sql查询结果中所有文档的score均为0，均未匹配成功</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/mysql/" rel="tag"># mysql</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/5b501ae4.html" rel="prev" title="mysql如何取出每个group的topN">
      <i class="fa fa-chevron-left"></i> mysql如何取出每个group的topN
    </a></div>
      <div class="post-nav-item">
    <a href="/94439062.html" rel="next" title="java类加载器问题思考与简单模拟热部署">
      java类加载器问题思考与简单模拟热部署 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E8%AF%8D%E4%B8%8E%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95"><span class="nav-number">1.</span> <span class="nav-text">分词与倒排索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql%E5%88%86%E8%AF%8D%E5%99%A8%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%85%A8%E6%96%87%E7%B4%A2%E5%BC%95%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="nav-number">2.</span> <span class="nav-text">mysql分词器配置与全文索引的创建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%A8%E6%96%87%E7%B4%A2%E5%BC%95%E7%9A%84%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="nav-number">3.</span> <span class="nav-text">全文索引的使用示例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">马天</p>
  <div class="site-description" itemprop="description">程序猿</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/matian2014" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;matian2014" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:matian1103@qq.com" title="E-Mail → mailto:matian1103@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2023001563号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">马天</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
